package com.cetcs.kmga.dataManager.service.impl.log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.asm.Type;
import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.common.DateUtil;
import com.cetcs.kmga.common.Page;
import com.cetcs.kmga.dataManager.dao.h2base.LogH2Query;
import com.cetcs.kmga.dataManager.dao.hbase.*;
import com.cetcs.kmga.dataManager.dao.mybatis.*;
import com.cetcs.kmga.dataManager.entity.dataManager.DataResourceCode;
import com.cetcs.kmga.dataManager.entity.dataManager.RiskEvent;
import com.cetcs.kmga.dataManager.entity.device.SecuRule;
import com.cetcs.kmga.dataManager.entity.device.vo.KeyAndValueVo;
import com.cetcs.kmga.dataManager.entity.global.AlarmCode;
import com.cetcs.kmga.dataManager.entity.global.CodeAndValue;
import com.cetcs.kmga.dataManager.entity.logManager.LogIndexQueryField;
import com.cetcs.kmga.dataManager.entity.logManager.LogQueryField;
import com.cetcs.kmga.dataManager.entity.logManager.ProbeLogIndex;
import com.cetcs.kmga.dataManager.entity.logManager.vi.*;
import com.cetcs.kmga.dataManager.entity.logManager.vo.LogTbFieldVo;
import com.cetcs.kmga.dataManager.entity.logManager.vo.LogTbKvVo;
import com.cetcs.kmga.dataManager.global.DeviceType;
import com.cetcs.kmga.dataManager.global.LogQueryEhcacheOper;
import com.cetcs.kmga.dataManager.global.ResLogType;
import com.cetcs.kmga.dataManager.service.feature.FeatureCodeService;
import com.cetcs.kmga.dataManager.service.log.LogManagerService;
import com.cetcs.kmga.dataManager.util.*;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * 日志操作服务层
 *
 * @author yyangs
 * @version V1.0 创建时间：2017-08-30 16:48
 * Copyright 2017 by CETC
 */
@Service
public class LogManagerServiceImpl implements LogManagerService {

    private final static Logger LOGGER = LoggerFactory.getLogger(LogManagerServiceImpl.class);
    @Autowired
    private LogQueryMapper logQueryMapper;

    @Autowired
    private ILogManagerDao logManagerDao;

    @Autowired
    private FeatureCodeMapper featureCodeMapper;

    @Autowired
    private LogH2Query logH2Query;

    @Autowired
    private FeatureCodeService featureCodeService;

    @Autowired
    private CodeTableManagerMapper codeTableManagerMapper;

    @Autowired
    private SecuRuleMapper secuRuleMapper;

    @Autowired
    private RiskEventMapper riskEventMapper;


    /**
     * 是否是模糊查询标志
     */
    private static final String LOG_INDEX_MFCX_ZT = "MFCX";

    private static Map<String,List<String>> CURRENT_TITLES_MAP=new HashMap<>();

    @Override
    public Map<String, Object> findLogQueryField(String code) throws Exception {
        String indexCode = ResLogType.getCode(code);
        //如果是以下几种统一将码值转换成探针Hbase的访问日志
        if (code.equals(ResLogType.MySQLFWRZ.toString()) || code.equals(ResLogType.MongoDBFWRZ.toString())
                || code.equals(ResLogType.FTPCJRZ.toString()) || code.equals(ResLogType.SolrFWRZ.toString())) {
            code = ResLogType.HbaseFWRZ.toString();
        }
        List<LogQueryField> dbQueryFields = logQueryMapper.findQueryFieldsByTbId(code);
        //开始组装数据
        //对数据进行分组
        //模糊查询条件
        List<Map<String, Object>> keyTypeList = Lists.newArrayList();
        //码表查询条件
        List<Map<String, Object>> dataList = Lists.newArrayList();
        //如果是风险事件
        if (ResLogType.FXSJ.toString().equals(code)) {
            List<DataResourceCode> resourceCodes = codeTableManagerMapper.getAllResourceTypeByCondition(new Data());
            List<KeyAndValueVo> newResourceCodes = resourceCodes.stream().map(p -> {
                KeyAndValueVo valueVo = new KeyAndValueVo();
                valueVo.setKey(p.getCode());
                valueVo.setValue(p.getValue());
                return valueVo;
            }).collect(Collectors.toList());
            Map<String, Object> resourceMap = convertResourceMap("资源类型", "rsType", "rsType", newResourceCodes);
            dataList.add(resourceMap);
            List<KeyAndValueVo> resources = logQueryMapper.findResourcesByResTypeCode(indexCode);
            resourceMap = convertResourceMap("资源名称", "ridStr", "ridStr", resources);
            dataList.add(resourceMap);
            List<SecuRule> secuRules = secuRuleMapper.getSecuRuleInfosByCondition(new Data());
            List<KeyAndValueVo> newsecuRules = secuRules.stream().map(p -> {
                KeyAndValueVo valueVo = new KeyAndValueVo();
                valueVo.setKey(String.valueOf(p.getId()));
                valueVo.setValue(String.valueOf(p.getName()));
                return valueVo;
            }).collect(Collectors.toList());
            resourceMap = convertResourceMap("规则名称", "ruleStr", "ruleStr", newsecuRules);
            dataList.add(resourceMap);
        } else if (ResLogType.HbaseFWRZ.toString().equals(code)) {
            List<KeyAndValueVo> resources = logQueryMapper.findResourcesByResTypeCode(indexCode);
            Map<String, Object> resourceMap = convertResourceMap("资源名称", indexCode, ProbeLogIndex.RESRC_ID.toString(), resources);
            dataList.add(resourceMap);
            List<KeyAndValueVo> sysClusters = featureCodeMapper.findAllSysClientClusters();
            Map<String, Object> sysClusterMap = convertResourceMap("客户端名称", indexCode, ProbeLogIndex.CLUSTER_ID.toString(), sysClusters);
            dataList.add(sysClusterMap);
            //探针设备日志，需要添加特例的设备数据
        } else if (ResLogType.TZSBRZ.toString().equals(code) ||
                ResLogType.TMSBRZ.toString().equals(code) ||
                ResLogType.YWSBRZ.toString().equals(code)) {
            String typeName = "";
            if (ResLogType.TZSBRZ.toString().equals(code)) {
                typeName = DeviceType.DEVICE_TZ.getTypeName();
            } else if (ResLogType.TMSBRZ.toString().equals(code)) {
                typeName = DeviceType.DEVICE_TM.getTypeName();
            } else if (ResLogType.YWSBRZ.toString().equals(code)) {
                typeName = DeviceType.DEVICE_YW.getTypeName();
            }
            List<AlarmCode> devices = featureCodeMapper.findDevicesByDeviceType(typeName);
            List<KeyAndValueVo> deviceList = devices.stream().map(p -> {
                KeyAndValueVo valueVo = new KeyAndValueVo();
                valueVo.setKey(p.getCode());
                valueVo.setValue(p.getValue());
                return valueVo;
            }).collect(Collectors.toList());
            Map<String, Object> resourceMap = convertResourceMap("来源设备", indexCode, "REPT_DEVICE_NUM", deviceList);
            dataList.add(resourceMap);
        }
        //其他查询条件
        List<Map<String, Object>> otherList = Lists.newArrayList();
        int dataId = 0;
        int otherId = 0;
        if (dbQueryFields != null && dbQueryFields.size() > 0) {
            for (int i = 0; i < dbQueryFields.size(); i++) {
                LogQueryField queryField = dbQueryFields.get(i);
                if (queryField.getLocalType() == null) {
                    continue;
                }
                //1、页面右上角；2、列表上部；3、其他
                switch (queryField.getLocalType().intValue()) {
                    case 1://页面右上角
                    {
                        Map<String, Object> keyTypeMap = new HashMap<>();
                        keyTypeMap.put("value", queryField.getFieldName());
                        keyTypeMap.put("name", queryField.getFieldInfo());
                        if (queryField.getIndexs() != null && queryField.getIndexs().size() > 0) {
                            LogIndexQueryField indexQueryField = queryField.getIndexs().get(0);
                            String markStr = "" + indexQueryField.getTbName();//拼接 isUnitKey
                            markStr += "," + indexQueryField.getRowKeyName();//拼接 rowKeySort
                            markStr += "," + indexQueryField.getRowKeyLen();//拼接 queryType
                            keyTypeMap.put("mark", markStr);
                        } else {
                            //是否是模糊查询
                            keyTypeMap.put("mark", LOG_INDEX_MFCX_ZT);
                        }
                        keyTypeList.add(keyTypeMap);
                        break;
                    }
                    case 2://列表上部
                    {
                        dataId++;
                        Map<String, Object> dataMap = new HashMap<>();
                        dataMap.put("stringId", queryField.getFieldName());
                        dataMap.put("name", queryField.getFieldInfo());
                        dataMap.put("id", dataId);
                        String markStr = "" + queryField.getIsUnitKey();//拼接 isUnitKey
                        markStr += "," + queryField.getRowKeySort();//拼接 rowKeySort
                        markStr += "," + queryField.getQueryType();//拼接 queryType
                        markStr += "," + queryField.getFieldLength();//拼接 fieldLength
                        dataMap.put("mark", markStr);
                        dataMap.put("showType", queryField.getShowType());
                        List<Map<String, Object>> valArr = Lists.newArrayList();
                        if (queryField.getCodes() != null && queryField.getCodes().size() > 0) {
                            if (queryField.getTypeId() != null && queryField.getTypeId().intValue() == 3) {
                                List<CodeAndValue> codeAndValues = featureCodeMapper.findAllOperTypeFeatureCodes();
                                for (int j = 0; j < queryField.getCodes().size(); j++) {
                                    Map<String, Object> valMap = new HashMap<>();
                                    AlarmCode singleCode = queryField.getCodes().get(j);
                                    List<String> operNames = codeAndValues.stream().filter(p -> p.getCode().equals(singleCode.getCode()))
                                            .map(p -> p.getValue()).collect(Collectors.toList());
                                    String operNameStr = StringUtils.join(operNames, " ");
                                    valMap.put("titles", operNameStr);
                                    valMap.put("values", singleCode.getCode());
                                    valMap.put("names", singleCode.getValue());
                                    valArr.add(valMap);
                                }
                            } else {
                                for (int j = 0; j < queryField.getCodes().size(); j++) {
                                    Map<String, Object> valMap = new HashMap<>();
                                    valMap.put("values", queryField.getCodes().get(j).getCode());
                                    valMap.put("names", queryField.getCodes().get(j).getValue());
                                    valMap.put("titles", queryField.getCodes().get(j).getValue());
                                    valArr.add(valMap);
                                }
                            }
                        }
                        dataMap.put("value", valArr);
                        dataList.add(dataMap);
                        break;
                    }
                    case 3://其他
                    {
                        otherId++;
                        Map<String, Object> otherMap = new HashMap<>();
                        otherMap.put("stringId", queryField.getFieldName());
                        otherMap.put("name", queryField.getFieldInfo());
                        otherMap.put("id", otherId);
                        String markStr = "" + queryField.getIsUnitKey();//拼接 isUnitKey
                        markStr += "," + queryField.getRowKeySort();//拼接 rowKeySort
                        markStr += "," + queryField.getQueryType();//拼接 queryType
                        markStr += "," + queryField.getFieldLength();//拼接 fieldLength
                        otherMap.put("mark", markStr);
                        otherMap.put("showType", queryField.getShowType());
                        List<Map<String, Object>> valArr = Lists.newArrayList();
                        Map<String, Object> valMap = new HashMap<>();
                        valMap.put("values", "");
                        valMap.put("names", queryField.getFieldInfo());
                        valArr.add(valMap);
                        if (queryField.getCodes() != null && queryField.getCodes().size() > 0) {
                            for (int j = 0; j < queryField.getCodes().size(); j++) {
                                valMap = new HashMap<>();
                                valMap.put("values", queryField.getCodes().get(j).getCode());
                                valMap.put("names", queryField.getCodes().get(j).getValue());
                                valArr.add(valMap);
                            }
                        }
                        otherMap.put("value", valArr);
                        otherList.add(otherMap);
                        break;
                    }
                }
            }
        }
        Map<String, Object> ret = new HashMap<>();
        ret.put("keyType", keyTypeList);
        ret.put("other", otherList);
        ret.put("list", dataList);
        return ret;
    }

    /**
     * 转换资源为资源map
     *
     * @param indexCode    索引编码
     * @param rsColumnName 字段
     * @return
     */
    private Map<String, Object> convertResourceMap(String name, String indexCode, String rsColumnName, List<KeyAndValueVo> resources) {
        //获取数据源资源信息
        Map<String, Object> resourceMap = new HashMap<>();
        resourceMap.put("stringId", rsColumnName);
        resourceMap.put("name", name);
        resourceMap.put("id", indexCode);
        List<Map<String, Object>> resArr = resources.stream().map(p -> {
            Map<String, Object> valMap = new HashMap<>();
            valMap.put("values", p.getKey());
            valMap.put("names", p.getValue());
            valMap.put("titles", p.getValue());
            return valMap;
        }).collect(Collectors.toList());
        resourceMap.put("value", resArr);
        return resourceMap;
    }

    /**
     * 根据探针日志的类型名称获取 探针日志用户名、数据库名、表名、客户端ip等纬表的数据
     *
     * @param pageType  类型
     * @param startTime 起始时间
     * @param endTime   结束时间
     * @param clientIp  客户端IP
     * @param userName  用户名称
     * @param dbName    数据库名称
     * @param tbName    表名称
     * @param clusterId 集群ID
     * @return
     */
    private List<LogTbKvVo> findLogTbKvVoList(String pageType, Date startTime, Date endTime, String clientIp, String userName, String dbName, String tbName, String clusterId) {
        //所有的参数都默认1=1,当不填的时候都是1=1
        //当pageType为探针Hbase日志时
        List<LogTbKvVo> ret = null;
        if (ResLogType.HbaseFWRZ.toString().equals(pageType)) {
            ret = logQueryMapper.findHbaseTbKvVoList(startTime, endTime, clientIp, userName, dbName, tbName, clusterId);
            //当为solr访问日志时
        } else if (ResLogType.SolrFWRZ.toString().equals(pageType)) {
            ret = logQueryMapper.findSolrTbKvVoList(startTime, endTime, clientIp, userName, dbName, tbName, clusterId);
            //当为MYsql访问日志时
        } else if (ResLogType.MySQLFWRZ.toString().equals(pageType)) {
            ret = logQueryMapper.findMySqlTbKvVoList(startTime, endTime, clientIp, userName, dbName, tbName, clusterId);
            //当为MongoDB访问日志时
        } else if (ResLogType.MongoDBFWRZ.toString().equals(pageType)) {
            ret = logQueryMapper.findMongoTbKvVoList(startTime, endTime, clientIp, userName, dbName, tbName, clusterId);
            //当为FTP访问日志时
        } else if (ResLogType.FTPCJRZ.toString().equals(pageType)) {
            ret = logQueryMapper.findFtpTbKvVoList(startTime, endTime, clientIp, userName, dbName, tbName, clusterId);
        }
        return ret;
    }

    /**
     * 根据参数字段找出相应的值
     *
     * @param indexSearchList 参数数组
     * @param strId           参数字段
     * @return
     */
    private String findProbeIndexParamKeywordByStrId(List<LogIndexSearch> indexSearchList, String strId) {
        String keyword = "1=1 limit 1";
        if (indexSearchList != null && indexSearchList.size() > 0) {
            List<LogIndexSearch> currentIndexList = indexSearchList.stream().filter(p -> strId.equalsIgnoreCase(p.getStringId())).collect(Collectors.toList());
            if (currentIndexList != null && currentIndexList.size() > 0) {
                String kv = currentIndexList.get(0).getValue();
                if (StringUtils.isNotBlank(kv)) {
                    keyword = kv;
                }
            }
        }
        return keyword;
    }

    /**
     * 判断探针审计日志是否发生填值
     *
     * @param record
     * @return
     */
    private boolean isHaveFuzzy(LogSearchBasicVi record) {
        boolean haveFuzzy = false;
        List<LogIndexSearch> indexSearchList = record.getTop1().getKeyWords();
        if (indexSearchList != null && indexSearchList.size() > 0) {
            for (int i = 0; i < indexSearchList.size(); i++) {
                if (StringUtils.isNotBlank(indexSearchList.get(i).getValue())) {
                    haveFuzzy = true;
                    break;
                }
            }
        }
        if (!haveFuzzy) {
            if (record.getTop2() != null && record.getTop2().size() > 0) {
                List<LogSearchProbeVi> clusterIndexList = record.getTop2().stream().filter(p -> ProbeLogIndex.CLUSTER_ID.toString().equalsIgnoreCase(p.getName())).collect(Collectors.toList());
                if (clusterIndexList != null && clusterIndexList.size() > 0) {
                    if (clusterIndexList.get(0).getVal() != null && clusterIndexList.get(0).getVal().length > 0) {
                        haveFuzzy = true;
                    }
                }
            }
        }
        return haveFuzzy;
    }


    private LogPage findLogFH2Database(List<List<String>> columnParamArr, List<String> columnArr, HBasePageModel fwrzRet,
                                       String pageType, List<LogTbFieldVo> listFlo, int page, int pageSize, boolean isHaveParam, LogSearchBasicVi record,String clientIp) {
        //开始创建内存数据库
        logH2Query.createLogCacheTb(columnParamArr,clientIp);
        //开始调用多线程向内存数据库中写数据
        System.out.println("======保存内存数据库起始时间==="+DateUtils.getTime());
        logH2Query.createBathLogDataUseThread(columnArr, fwrzRet.getDataList(),clientIp,record.isNotExport());
        System.out.println("======保存内存数据库结束时间==="+DateUtils.getTime());
        List<List<String>> queryColumnArr = listFlo.stream().map(p -> {
            List<String> singleCodes = Lists.newArrayList();
            singleCodes.add(p.getFieldName());
            singleCodes.add(String.valueOf(p.getIsCode()));
            singleCodes.add(p.getCodeType());
            return singleCodes;
        }).collect(Collectors.toList());
        Map<String, Integer> sortMap = queryLogTbSortMap(pageType);
        List<H2QueryIn> queryIns = null;
        if (isHaveParam) {
            queryIns = convertQueryIn(record);
        }
        LogPage ret = logH2Query.queryCacheDataFromTb(queryIns, sortMap, queryColumnArr, page, pageSize,clientIp);
        ret.setColumnArr(queryColumnArr);
        return ret;
    }

    private boolean isHaveSameKey(LogSearchBasicVi record, LogPage cacheRet) {
        String pageType = record.getPageType();
        boolean isHaveSameKey = false;
        if (cacheRet != null && pageType.equals(cacheRet.getPageType())) {
            String newKey = record.toString();
            if (newKey.equals(cacheRet.getLogQueryKey())) {
                isHaveSameKey = true;
            }
        }
        return isHaveSameKey;
    }

    @Override
    public LogPage findDeviceLogByPage(LogSearchBasicVi record) throws ExecutionException, InterruptedException, IllegalAccessException, KmgaDefinedException {
        Date searchStarTime = new Date();
        String clientIp=record.getClientIp();
        String mapKey=LogQueryEhcacheOper.gerMapKey(clientIp);
        LogPage LOG_CACHE_POJO = LogQueryEhcacheOper.getLogPage(clientIp);
        LogPage ret = new LogPage();
        //内存数据库表名
        int pageSize = record.getPageSize();
        record.getShowNum().sort((v1, v2) -> v1.compareTo(v2));
        int page = record.getPage();
        String pageType = record.getPageType();
        //先判断缓存对象是否存在，并判断缓存对象的查询条件是否相同
        if (isHaveSameKey(record, LOG_CACHE_POJO)) {
            //对象存在且之前的rowKey一样，只需要从内存数据库中查询即可
            List<List<String>> columnArr = LOG_CACHE_POJO.getColumnArr();
            Map<String, Integer> sortMap = queryLogTbSortMap(pageType);
            List<H2QueryIn> queryIns = convertQueryIn(record);
            LOGGER.error("=======走内存查询===="+record.getPageType());
            ret = logH2Query.queryCacheDataFromTb(queryIns, sortMap, columnArr, page, pageSize,clientIp);
            if (record.getShowNum() == null || record.getShowNum().size() == 0) {
                ret.setDisplay(LOG_CACHE_POJO.getDisplay());
            } else {
                ret.setDisplay(record.getShowNum());
                LOG_CACHE_POJO.setDisplay(record.getShowNum());
            }
            List<String> titles = CURRENT_TITLES_MAP.get(mapKey);
            ret.setTableHead(titles);
            LogQueryEhcacheOper.putLogPage(LOG_CACHE_POJO,clientIp);
        } else {
            //默认近一天的数据
            Date startTime = DateUtils.getTodayBefore(1);
            Date endTime = new Date();
            if (StringUtils.isNotBlank(record.getTop1().getDateRange())) {
                String dateRange = record.getTop1().getDateRange();
                String[] dateArr = dateRange.split("-");
                String startTimeStr = dateArr[0].trim() + "-" + dateArr[1].trim() + "-" + dateArr[2].trim();
                startTime = DateUtils.parse(startTimeStr, DateUtils.DATE_FORMAT_7);
                String endTimeStr = dateArr[3].trim() + "-" + dateArr[4].trim() + "-" + dateArr[5].trim();
                endTime = DateUtils.parse(endTimeStr, DateUtils.DATE_FORMAT_7);
            }
            if (!ResLogType.FXSJ.toString().equals(pageType)) {
                int days = DateUtils.daysBetween(startTime, endTime);
                if (days > 7) {
                    throw new KmgaDefinedException("查询天数超过七天，请重新设置！");
                }
            }
            HBasePageModel pageModel = new HBasePageModel(record.getPageSize());
            pageModel.setPageIndex(record.getPage());
            List<String> titles = Lists.newArrayList();
            List<Integer> display = Lists.newArrayList();
            List<List<String>> data = Lists.newArrayList();
            //如果没有数据
            boolean isHaveData = false;
            String newPageType = "";
            if (pageType.equals(ResLogType.HbaseFWRZ.toString()) || pageType.equals(ResLogType.MySQLFWRZ.toString()) || pageType.equals(ResLogType.MongoDBFWRZ.toString())
                    || pageType.equals(ResLogType.FTPCJRZ.toString()) || pageType.equals(ResLogType.SolrFWRZ.toString())) {
                newPageType = ResLogType.HbaseFWRZ.toString();
            } else {
                newPageType = pageType;
            }
            List<LogTbFieldVo> tbFieldList = logQueryMapper.findLogTbFieldInfoByTbId(newPageType);
            tbFieldList.forEach(p -> {
                if (ProbeLogIndex.RESRC_ID.toString().equals(p.getFieldName())) {
                    String tbCode = ResLogType.getCode(pageType);
                    p.setCodeType(tbCode);
                }
            });
            //找出只用做列表展示的页面
            List<LogTbFieldVo> listFlo = tbFieldList.stream().filter(p -> p.getIsList() != null && p.getIsList().intValue() == 1).collect(Collectors.toList());
            //循环展示数组
            for (int i = 0; i < listFlo.size(); i++) {
                if (listFlo.get(i).getIsShow() != null && listFlo.get(i).getIsShow().intValue() == 1) {
                    display.add(i);
                }
                titles.add(listFlo.get(i).getFieldInfo());
            }
            //开始查询风险事件日志
            if (ResLogType.FXSJ.toString().equals(newPageType)) {
                Data dataParam = new Data();
                dataParam.put("startTime", DateUtils.format(startTime, DateUtils.DATE_FORMAT_2));
                dataParam.put("endTime", DateUtils.format(endTime, DateUtils.DATE_FORMAT_2));
                List<LogIndexSearch> searchList = record.getTop1().getKeyWords();
                if (searchList != null && searchList.size() > 0) {
                    for (int i = 0; i < record.getTop1().getKeyWords().size(); i++) {
                        LogIndexSearch indexSearch = record.getTop1().getKeyWords().get(i);
                        String markStr = indexSearch.getMark();
                        String keyVal = indexSearch.getValue();
                        String strId = indexSearch.getStringId();
                        if (StringUtils.isNotBlank(markStr) && StringUtils.isNotBlank(keyVal)) {
                            dataParam.put(strId, keyVal);
                        }
                    }
                }
                List<LogSearchProbeVi> top2List = record.getTop2();
                if (top2List != null && top2List.size() > 0) {
                    for (int i = 0; i < top2List.size(); i++) {
                        LogSearchProbeVi probeVi = top2List.get(i);
                        if (probeVi.getVal() != null && probeVi.getVal().length > 0) {
                            String newKeyWord = probeVi.getVal()[0];
                            dataParam.put(probeVi.getName(), newKeyWord);
                        }
                    }
                }
                if (record.getTop3() != null && record.getTop3().getResult() != null && record.getTop3().getResult().size() > 0) {
                    for (int i = 0; i < record.getTop3().getResult().size(); i++) {
                        List<String> top3Res = record.getTop3().getResult().get(i);
                        if (top3Res != null && StringUtils.isNotBlank(top3Res.get(1))) {
                            dataParam.put(top3Res.get(0), top3Res.get(1));
                        }
                    }
                }
//                PageBounds pageBounds = new PageBounds(page, pageSize);
//                PageList<RiskEvent> eventList = riskEventMapper.findRiskEventListByDataParams(dataParam, pageBounds);
                Integer startNum = (page - 1) * pageSize + 1;
                dataParam.put("startNum", startNum);
                dataParam.put("pageSize", pageSize);
                List<RiskEvent> eventList = riskEventMapper.findRiskEventListByDataParams(dataParam);
                Integer totalNum = riskEventMapper.findRiskEventTotalNumByDataParams(dataParam);
                if (eventList != null && eventList.size() > 0) {
                    eventList.forEach(p -> {
                        if (StringUtils.isNotBlank(p.getLogType())) {
                            String newType = ResLogType.getResLogType(p.getLogType()).toString();
                            p.setLogType(newType);
                        }
                        //转码 等级
                        String level=p.getLevel();
                        if(StringUtils.isNotBlank(level)){
                            level = featureCodeService.acquireValueByCodeAndTypeId("5", level);
                        }
                        p.setLevel(level);
                        //转码 操作类型
                        String opType=p.getOperType();
                        if(StringUtils.isNotBlank(opType)){
                            opType = featureCodeService.acquireValueByCodeAndTypeId("3", opType);
                        }
                        p.setOperType(opType);
                        //转码 操作结果
                        String result=p.getActResult();
                        if(StringUtils.isNotBlank(result)){
                            result = featureCodeService.acquireValueByCodeAndTypeId("16", result);
                        }
                        p.setActResult(result);
                        //转码 规则
                        String rule=p.getRuleId();
                        if(StringUtils.isNotBlank(rule)){
                            SecuRule rulePojo=secuRuleMapper.findSecuRuleById(rule);
                            if(rulePojo!=null){
                                rule = rulePojo.getName();
                            }
                        }
                        p.setRuleId(rule);
                    });
                    List<String> queryColumnArr = listFlo.stream().map(p -> p.getFieldName()).collect(Collectors.toList());
                    List<List<String>> allData = Lists.newArrayList();
                    for (int j = 0; j < eventList.size(); j++) {
                        List<String> singleData = Lists.newArrayList();
                        RiskEvent re = eventList.get(j);
                        for (int i = 0; i < queryColumnArr.size(); i++) {
                            Object cVal = FieldUtils.readDeclaredField(re, queryColumnArr.get(i), true);
                            if (cVal != null) {
                                singleData.add(String.valueOf(cVal));
                            } else {
                                singleData.add("");
                            }
                        }
                        allData.add(singleData);
                    }
//                    Page newPage = new Page(eventList);
//                    ret.setTotalRecord(newPage.getTotalRecord( ));
//                    ret.setTotalPageCount(newPage.getTotalPageCount( ));
                    ret.setTotalRecord(totalNum);
                    Integer pageCount = totalNum % pageSize == 0 ? totalNum / pageSize : (totalNum / pageSize) + 1;
                    ret.setTotalPageCount(pageCount);
                    ret.setContent(allData);
                    ret.setTableHead(titles);
                    ret.setDisplay(display);
                    return ret;
                } else {
                    ret.setTableHead(titles);
                    ret.setDisplay(display);
                    ret.setContent(Lists.newArrayList());
                    return ret;
                }
                //探针审计日志查询
            } else if (pageType.equals(ResLogType.HbaseFWRZ.toString()) || pageType.equals(ResLogType.MySQLFWRZ.toString()) || pageType.equals(ResLogType.MongoDBFWRZ.toString())
                    || pageType.equals(ResLogType.FTPCJRZ.toString()) || pageType.equals(ResLogType.SolrFWRZ.toString())) {
                //开始查询纬表里面的索引
                boolean isHaveFuzzy = isHaveFuzzy(record);
                //集群ID
                String clusterId = "";
                List<LogSearchProbeVi> top2List = record.getTop2();
                List<LogTbKvVo> accurateIndexParams = Lists.newArrayList();
                if (top2List != null && top2List.size() > 0) {
                    List<LogSearchProbeVi> clusterIndexList = top2List.stream().filter(p -> ProbeLogIndex.CLUSTER_ID.toString().equalsIgnoreCase(p.getName())).collect(Collectors.toList());
                    if (clusterIndexList != null && clusterIndexList.size() > 0) {
                        String[] clusValArr = clusterIndexList.get(0).getVal();
                        if (clusValArr != null && clusValArr.length > 0) {
                            clusterId = clusValArr[0];
                        }
                    }
                    //探针日志查询以及对象的封装
                    top2List.removeAll(clusterIndexList);
                    //找出字段值不为空并转换相应的对象
                    List<LogTbKvVo> accurateTop2IndexList = top2List.stream().filter(p -> p.getVal() != null && p.getVal().length > 0).map(p -> {
                        LogTbKvVo singleKv = new LogTbKvVo();
                        singleKv.setKey(p.getName());
                        singleKv.setValue(p.getVal()[0]);
                        return singleKv;
                    }).collect(Collectors.toList());
                    List<List<String>> top3Res = record.getTop3().getResult();
                    if (accurateTop2IndexList != null && accurateTop2IndexList.size() > 0) {
                        accurateIndexParams.addAll(accurateTop2IndexList);
                    }
                    if (top3Res != null && top3Res.size() > 0) {
                        List<LogTbKvVo> accurateTop3IndexList = top3Res.stream().filter(p -> StringUtils.isNotBlank(p.get(1))).map(p -> {
                            LogTbKvVo singleKv = new LogTbKvVo();
                            singleKv.setKey(p.get(0));
                            String kStr="0";
                            if("success".equals(p.get(1))){
                                kStr="1";
                            }
                            singleKv.setValue(kStr);
                            return singleKv;
                        }).collect(Collectors.toList());
                        if (accurateTop3IndexList != null && accurateTop3IndexList.size() > 0) {
                            accurateIndexParams.addAll(accurateTop3IndexList);
                        }
                    }
                }
                //组装索引查询的字段
                List<String> columnArr = tbFieldList.stream().map(p -> p.getFieldName()).collect(Collectors.toList());
                List<List<String>> columnParamArr = tbFieldList.stream().map(p -> {
                    List<String> params = Lists.newArrayList();
                    params.add(p.getFieldName());
                    params.add(p.getFieldLength());
                    params.add(p.getFieldType());
                    return params;
                }).collect(Collectors.toList());
                String codeType = ResLogType.getCode(pageType);
                if (isHaveFuzzy) {
                    List<LogIndexSearch> indexSearchList = record.getTop1().getKeyWords();
                    //客户端IP
                    String cip = findProbeIndexParamKeywordByStrId(indexSearchList, ProbeLogIndex.CLIENT_IP.toString());
                    //用户名称
                    String userName = findProbeIndexParamKeywordByStrId(indexSearchList, ProbeLogIndex.USR.toString());
                    //数据库名称
                    String dbName = findProbeIndexParamKeywordByStrId(indexSearchList, ProbeLogIndex.DB_NAME.toString());
                    //表名称
                    String tbName = findProbeIndexParamKeywordByStrId(indexSearchList, ProbeLogIndex.TBL_NAME.toString());
                    List<LogTbKvVo> logTbKvVoList = findLogTbKvVoList(pageType, startTime, endTime, cip, userName, dbName, tbName, clusterId);
                    //如果索引结果没有数据就直接返回结果
                    if (logTbKvVoList == null || logTbKvVoList.size() == 0) {
                        isHaveData = false;
                    } else {
                        HBasePageModel fwrzRet = logManagerDao.findProbeInfoByCondition(codeType, columnArr, startTime, endTime, logTbKvVoList, accurateIndexParams,record.isNotExport());

                        //开始查询
                        if (fwrzRet != null && fwrzRet.getDataList() != null && fwrzRet.getDataList().size() != 0) {
                            ret = findLogFH2Database(columnParamArr, columnArr, fwrzRet, pageType, listFlo, page, pageSize, false, record,clientIp);
                            isHaveData = true;
                        }
                    }
                    //当一个模糊匹配参数都没有输入的时候
                } else {
                    List<LogTbKvVo> fuzzyIndexParams = null;
                    if (accurateIndexParams != null && accurateIndexParams.size() > 0) {
                        fuzzyIndexParams = Lists.newArrayList();
                        LogTbKvVo singleKv = new LogTbKvVo();
                        StringBuffer kvBuffer = new StringBuffer();
                        StringBuffer vaBuffer = new StringBuffer();
                        for (int i = 0; i < 37; i++) {
                            kvBuffer.append("\\x00");
                            if (i != 0) {
                                vaBuffer.append(",");
                            }
                            vaBuffer.append("1");
                        }
                        singleKv.setKey(kvBuffer.toString());
                        singleKv.setValue(vaBuffer.toString());
                        fuzzyIndexParams.add(singleKv);
                    } else {
                        accurateIndexParams = null;
                    }
                    HBasePageModel fwrzRet = logManagerDao.findProbeInfoByCondition(codeType, columnArr, startTime, endTime, fuzzyIndexParams, accurateIndexParams,record.isNotExport());
                    if (fwrzRet != null && fwrzRet.getDataList() != null && fwrzRet.getDataList().size() != 0) {
                        ret = findLogFH2Database(columnParamArr, columnArr, fwrzRet, pageType, listFlo, page, pageSize, false, record,clientIp);
                        isHaveData = true;
                    }
                }
                //其他的查询，包括脱敏审计日志、探针设备日志等设备日志
            } else {
                List<String> columnArr = tbFieldList.stream().map(p -> p.getFieldName()).collect(Collectors.toList());
                List<List<String>> columnParamArr = tbFieldList.stream().map(p -> {
                    List<String> params = Lists.newArrayList();
                    params.add(p.getFieldName());
                    params.add(p.getFieldLength());
                    params.add(p.getFieldType());
                    return params;
                }).collect(Collectors.toList());
                Map<String, Object> indexParams = null;
                if (record.getQueryType() != null && record.getQueryType().intValue() == 0) {
                    List<LogIndexSearch> indexSearchList = record.getTop1().getKeyWords();
                    if (indexSearchList != null && indexSearchList.size() > 0) {
                        for (int i = 0; i < indexSearchList.size(); i++) {
                            LogIndexSearch indexSearch = indexSearchList.get(i);
                            String markStr = indexSearch.getMark();
                            if (StringUtils.isNotBlank(markStr) && !LOG_INDEX_MFCX_ZT.equals(markStr) &&
                                    StringUtils.isNotBlank(indexSearch.getValue())) {
                                indexParams = new HashMap<>();
                                String[] markArr = markStr.split(",");
                                indexParams.put("rowKeyLength", markArr[2]);
                                indexParams.put("keyword", indexSearch.getValue());
                                indexParams.put("tbName", markArr[0]);
                                indexParams.put("rowKeyName", markArr[1]);
                                break;
                            }
                        }
                    }
                }
                HBasePageModel fwrzRet = null;
                //脱敏审计日志-Hbase静态脱敏
                String tbName = ResLogType.getTableName(pageType);
                if (pageType.equals(ResLogType.JTTMHbase.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, ResLogType.getCode(pageType), columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //脱敏审计日志-Solr静态脱敏
                } else if (pageType.equals(ResLogType.JTTMSolr.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, ResLogType.getCode(pageType), columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //脱敏审计日志-MySql静态脱敏
                } else if (pageType.equals(ResLogType.JTTMMySql.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, ResLogType.getCode(pageType), columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //脱敏审计日志-Mysql动态脱敏
                } else if (pageType.equals(ResLogType.DTTMMySql.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, ResLogType.getCode(pageType), columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //脱敏审计日志-文件静态脱敏
                } else if (pageType.equals(ResLogType.WJJTTM.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, ResLogType.getCode(pageType), columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //脱敏审计日志-文件动态脱敏
                } else if (pageType.equals(ResLogType.WJDTTM.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, ResLogType.getCode(pageType), columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //监管平台用户日志
                } else if (pageType.equals(ResLogType.JGPTRZ.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, null, columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //脱敏设备日志
                } else if (pageType.equals(ResLogType.TMSBRZ.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, null, columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //获取运维用户操作日志
                } else if (pageType.equals(ResLogType.YWSBRZ.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, null, columnArr, startTime, endTime, indexParams,record.isNotExport());
                    //运维脱敏设备日志
                } else if (pageType.equals(ResLogType.YWCZRZ.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, null, columnArr, startTime, endTime, indexParams,record.isNotExport());
                } else if (pageType.equals(ResLogType.TZSBRZ.toString())) {
                    fwrzRet = logManagerDao.findLogInfoByCondition(tbName, null, columnArr, startTime, endTime, indexParams,record.isNotExport());
                }

                //开始查询
                if (fwrzRet != null && fwrzRet.getDataList() != null && fwrzRet.getDataList().size() != 0) {
                    //开始创建内存数据库
                    logH2Query.createLogCacheTb(columnParamArr,clientIp);
                    //开始调用多线程向内存数据库中写数据
                    logH2Query.createBathLogDataUseThread(columnArr, fwrzRet.getDataList(),clientIp,record.isNotExport());
                    List<List<String>> queryColumnArr = listFlo.stream().map(p -> {
                        List<String> singleCodes = Lists.newArrayList();
                        singleCodes.add(p.getFieldName());
                        singleCodes.add(String.valueOf(p.getIsCode()));
                        singleCodes.add(p.getCodeType());
                        return singleCodes;
                    }).collect(Collectors.toList());
                    Map<String, Integer> sortMap = queryLogTbSortMap(pageType);
                    List<H2QueryIn> queryIns = convertQueryIn(record);
                    ret = logH2Query.queryCacheDataFromTb(queryIns, sortMap, queryColumnArr, page, pageSize,clientIp);
                    ret.setColumnArr(queryColumnArr);
                    isHaveData = true;
                } else {
                    //如果不为空需要将缓存数据清理掉
                    if (LOG_CACHE_POJO != null) {
                        LogQueryEhcacheOper.remvoeLogPage();
                    }
                }
            }
            ret.setTableHead(titles);
            ret.setDisplay(display);
            if (data.size() != 0) {
                ret.setTotalPageCount(0);
                ret.setTotalRecord(0);
            }
            //如果不是初次加载，需要返回前端选择显示框
            if (record.getFirstShow() != null && 0 != record.getFirstShow().intValue()) {
                ret.setDisplay(record.getShowNum());
            }
            //如果没有数据不能设置缓存
            if (isHaveData) {
                ret.setPageType(pageType);
                ret.setLogQueryKey(record.toString());
                ret.setPageNumber(record.getPage());
                ret.setPageSize(record.getPageSize());
                LogQueryEhcacheOper.putLogPage(ret,clientIp);
                CURRENT_TITLES_MAP.put(mapKey,titles);
            } else {
                ret.setContent(Lists.newArrayList());
                if (LOG_CACHE_POJO != null) {
                    LogQueryEhcacheOper.remvoeLogPage();
                }
                CURRENT_TITLES_MAP.put(mapKey,titles);
            }
        }
        Date searchEndTime = new Date();
        double interval = (searchEndTime.getTime() - searchStarTime.getTime()) / 1000.0;
        DecimalFormat df = new DecimalFormat("#.##");
        String intervalStr = df.format(interval) + "秒";
        LOGGER.error("==========查询时间：" + intervalStr + "================");
        return ret;
    }

    @Override
    public LogPage findExportLogData(LogSearchBasicVi record) throws ExecutionException, InterruptedException, IllegalAccessException, KmgaDefinedException {
        int maxPageSize = 5000;
        if (record.getPageSize() > 5000) {
            maxPageSize = 5000;
        }
        record.setPageSize(maxPageSize);
        LogPage logPage = findDeviceLogByPage(record);
        List<String> thList = logPage.getTableHead();
        List<String> titles = Lists.newArrayList();
        List<Integer> display = logPage.getDisplay();
        for (int i = 0; i < display.size(); i++) {
            String tName = thList.get(display.get(i));
            titles.add(tName);
        }
        List<List<String>> content = logPage.getContent();
        //
        List<List<String>> ret = Lists.newArrayList();
        //如果有数据的话
        if (content != null && content.size() > 0) {
            ret = content.stream().map(p -> {
                List<String> singleValList = Lists.newArrayList();
                for (int i = 0; i < display.size(); i++) {
                    String tName = p.get(display.get(i));
                    singleValList.add(tName);
                }
                return singleValList;
            }).collect(Collectors.toList());
        }
        logPage.setTableHead(titles);
        logPage.setContent(ret);
        return logPage;
    }

    private List<H2QueryIn> convertQueryIn(LogSearchBasicVi record) {
        List<H2QueryIn> queryIns = Lists.newArrayList();
        LogSearchTopVi topVi_1 = record.getTop1();
        //取出没有索引表且只能从h2数据库中模糊匹配的字段
        if (topVi_1 != null && topVi_1.getKeyWords() != null && topVi_1.getKeyWords().size() > 0) {
            List<LogIndexSearch> tpKeyList = topVi_1.getKeyWords();
            for (int i = 0; i < tpKeyList.size(); i++) {
                LogIndexSearch indexSearch = record.getTop1().getKeyWords().get(i);
                String markStr = indexSearch.getMark();
                String keyVal = indexSearch.getValue();
                String strId = indexSearch.getStringId();
                //如果是查询条件等于
                if (record.getQueryType() != null) {
                    if (StringUtils.isNotBlank(keyVal)) {
                        H2QueryIn h2Query = new H2QueryIn();
                        h2Query.setColumnName(strId);
                        h2Query.setKeywords(keyVal);
                        h2Query.setQueryType(IQueryConf.QUERY_TYPE_LIKE);
                        queryIns.add(h2Query);
                    }
                } else {
                    if (StringUtils.isNotBlank(keyVal) && StringUtils.isNotBlank(markStr) && LOG_INDEX_MFCX_ZT.equals(markStr)) {
                        H2QueryIn h2Query = new H2QueryIn();
                        h2Query.setColumnName(strId);
                        h2Query.setKeywords(keyVal);
                        h2Query.setQueryType(IQueryConf.QUERY_TYPE_LIKE);
                        queryIns.add(h2Query);
                    }
                }
            }
        }
        List<LogSearchProbeVi> top2List = record.getTop2();
        if (top2List != null && top2List.size() > 0) {
            for (int i = 0; i < top2List.size(); i++) {
                LogSearchProbeVi probeVi = top2List.get(i);
                if (probeVi.getVal() != null && probeVi.getVal().length > 0) {
                    H2QueryIn h2Query = new H2QueryIn();
                    h2Query.setColumnName(probeVi.getName());
                    h2Query.setKeywords(probeVi.getVal()[0]);
                    h2Query.setQueryType(IQueryConf.QUERY_TYPE_LIKE);
                    queryIns.add(h2Query);
                }
            }
        }
        if (record.getTop3() != null && record.getTop3().getResult() != null && record.getTop3().getResult().size() > 0) {
            for (int i = 0; i < record.getTop3().getResult().size(); i++) {
                List<String> top3Res = record.getTop3().getResult().get(i);
                if (top3Res != null && StringUtils.isNotBlank(top3Res.get(1))) {
                    H2QueryIn h2Query = new H2QueryIn();
                    h2Query.setColumnName(top3Res.get(0));
                    h2Query.setKeywords(top3Res.get(1));
                    h2Query.setQueryType(IQueryConf.QUERY_TYPE_EQUAL);
                    queryIns.add(h2Query);
                }
            }
        }
        if (record.getTop3() != null && record.getTop3().getValue() != null && record.getTop3().getValue().size() > 0) {
            for (int i = 0; i < record.getTop3().getValue().size(); i++) {
                LogSearchOtherBackVi top3Res = record.getTop3().getValue().get(i);
                if (top3Res.getVal() != null && top3Res.getVal().size() > 0
                        && StringUtils.isNotBlank(top3Res.getVal().get(0))
                        && StringUtils.isNotBlank(top3Res.getVal().get(1))) {
                    H2QueryIn h2Query = new H2QueryIn();
                    h2Query.setColumnName(top3Res.getFlag());
                    String keyword = top3Res.getFlag() + " >='" + top3Res.getVal().get(0) + "' and " + top3Res.getFlag() + "<='" + top3Res.getVal().get(1) + "'";
                    h2Query.setKeywords(keyword);
                    h2Query.setQueryType(IQueryConf.QURYY_TYPE_DEFINED);
                    queryIns.add(h2Query);
                }
            }
        }
        return queryIns;
    }

    private LogPage convertLogPage(List<List<String>> data, int page, int pageSize, List<List<String>> queryColumnArr) {
        LogPage ret = new LogPage();
        int totalRecord = data.size();
        int pageCount = totalRecord % pageSize == 0 ? totalRecord % pageSize : totalRecord % pageSize + 1;
        ret.setTotalRecord(totalRecord);
        ret.setPageNumber(page);
        ret.setPageSize(pageSize);
        ret.setTotalPageCount(pageCount);
        List<List<String>> children = subListChildren(page, pageSize, data);
        for (int j = 0; j < children.size(); j++) {
            List<String> fwrzList = children.get(j);
            List<String> newFwrzList = Lists.newArrayList();
            for (int i = 0; i < queryColumnArr.size(); i++) {
                String val = fwrzList.get(i);
                if (!StringUtils.isEmpty(fwrzList.get(i))) {
                    List<String> qKList = queryColumnArr.get(i);
                    String isCode = qKList.get(1);
                    if ("1".equals(isCode)) {
                        String codeType = qKList.get(2);
                        String newVal = featureCodeService.acquireValueByCodeAndTypeId(codeType, val);
                        newFwrzList.add(newVal);
                    } else {
                        newFwrzList.add(val);
                    }
                } else {
                    newFwrzList.add("");
                }
            }
            children.remove(j);
            children.add(j, newFwrzList);
        }
        ret.setContent(children);
        return ret;
    }

    /**
     * 获取排序的列表
     *
     * @param pageType
     * @return
     */
    private Map<String, Integer> queryLogTbSortMap(String pageType) {
        String newPageType = "";
        if (pageType.equals(ResLogType.HbaseFWRZ.toString()) || pageType.equals(ResLogType.MySQLFWRZ.toString()) || pageType.equals(ResLogType.MongoDBFWRZ.toString())
                || pageType.equals(ResLogType.FTPCJRZ.toString()) || pageType.equals(ResLogType.SolrFWRZ.toString())) {
            newPageType = ResLogType.HbaseFWRZ.toString();
        } else {
            newPageType = pageType;
        }
        List<KeyAndValueVo> kvList = logQueryMapper.findTbSortFieldsByCode(newPageType);
        Map<String, Integer> ret = null;
        if (kvList != null & kvList.size() > 0) {
            ret = new HashMap<>();
            for (KeyAndValueVo valueVo : kvList) {
                ret.put(valueVo.getKey(), Integer.parseInt(valueVo.getValue()));
            }
        }
        return ret;
    }

    @Override
    public Map<String, Object> findLogDetailById(String id, String pageType, String time) {
        Map<String, Object> ret = new HashMap<>();
        String newPageType = "";
        String tbName = "";
        int flag = 0;
        if (pageType.equals(ResLogType.HbaseFWRZ.toString()) || pageType.equals(ResLogType.MySQLFWRZ.toString()) || pageType.equals(ResLogType.MongoDBFWRZ.toString())
                || pageType.equals(ResLogType.FTPCJRZ.toString()) || pageType.equals(ResLogType.SolrFWRZ.toString())) {
            newPageType = ResLogType.HbaseFWRZ.toString();
            String stName = ResLogType.getTableName(pageType);
            String code = ResLogType.getCode(pageType);
            String timeStr = DateUtils.format(new Date(), DateUtils.DATE_FORMAT_10);
            if (StringUtils.isNotBlank(time)) {
                timeStr = DateUtils.format(DateUtils.parse(time, DateUtils.DATE_FORMAT_2), DateUtils.DATE_FORMAT_10);
            }
            tbName = stName + "_" + code + "_" + timeStr;
        } else {
            newPageType = pageType;
            tbName = ResLogType.getTableName(pageType);
            if (pageType.equals(ResLogType.WJJTTM) || pageType.equals(ResLogType.WJDTTM)) {
                flag = 1;
            } else {
                flag = 2;
            }
        }
        List<LogTbFieldVo> tbFieldList = logQueryMapper.findLogTbFieldInfoByTbId(newPageType);
        tbFieldList.forEach(p -> {
            if (ProbeLogIndex.RESRC_ID.toString().equals(p.getFieldName())) {
                String tbCode = ResLogType.getCode(pageType);
                p.setCodeType(tbCode);
            }
        });
        List<List<String>> columnArr = tbFieldList.stream().map(p -> {
            List<String> singleCodes = Lists.newArrayList();
            singleCodes.add(p.getFieldName());
            singleCodes.add(String.valueOf(p.getIsCode()));
            singleCodes.add(p.getCodeType());
            return singleCodes;
        }).collect(Collectors.toList());
        Map<String, Object> dbRet = logManagerDao.findLogDetailById(tbName, id);
        if (dbRet != null) {
            for (List<String> k : columnArr) {
                String currName = k.get(0);
                Object val = dbRet.get(k.get(0));
                if (val == null || StringUtils.isEmpty(String.valueOf(val))) {
                    ret.put(currName, "");
                } else {
                    String isCode = k.get(1);
                    if ("DESE_DETAIL".equals(currName)) {
                        int newFlag = flag;
                        String deseDetailStr = String.valueOf(val);
                        List<Map> detailList = null;
                        try {
                            detailList = JSON.parseArray(deseDetailStr, Map.class);
                        } catch (Exception ex) {
                            detailList = Lists.newArrayList();
                        }
                        List<Map<String, Object>> detailRet = Lists.newArrayList();
                        if (newFlag == 1) {
                            for (Map p : detailList) {
                                Map<String, Object> singleDetail = new HashMap<>();
                                String field = p.get("mask_type") == null ? "" : String.valueOf(p.get("mask_type"));
                                String deseName = p.get("mask_desc") == null ? "" : String.valueOf(p.get("mask_desc"));
                                singleDetail.put("field", field);
                                singleDetail.put("deseName", deseName);
                                detailRet.add(singleDetail);
                            }
                            ret.put(currName, detailRet);
                        } else if (newFlag == 2) {
                            Map<String, Map<String, List<String>>> descMap = new HashMap<>();
                            for (Map p : detailList) {
                                String field = String.valueOf(p.get("field_name"));
                                if (field == null) {
                                    continue;
                                }
                                String maskDesc = p.get("mask_desc") == null ? "" : String.valueOf(p.get("mask_desc"));
                                String maskType = p.get("mask_type") == null ? "" : String.valueOf(p.get("mask_type"));
                                if (descMap.get(field) == null) {
                                    if (StringUtils.isNotBlank(maskType)) {
                                        Map<String, List<String>> markMapList = new HashMap<>();
                                        List<String> descList = Lists.newLinkedList();
                                        descList.add(maskDesc);
                                        markMapList.put(maskType, descList);
                                        descMap.put(field, markMapList);
                                    }
                                } else {
                                    Map<String, List<String>> oldMarkMapList = descMap.get(field);
                                    if (StringUtils.isNotBlank(maskType)) {
                                        List<String> oldDesList = oldMarkMapList.get(maskType);
                                        if (oldDesList != null) {
                                            oldDesList.add(maskDesc);
                                            oldMarkMapList.put(maskType, oldDesList);
                                        } else {
                                            List<String> descList = Lists.newLinkedList();
                                            descList.add(maskDesc);
                                            oldMarkMapList.put(maskType, descList);
                                        }
                                    }
                                }
                            }
                            descMap.forEach((p, v) -> {
                                Map<String, Object> singleDetail = new HashMap<>();
                                singleDetail.put("field", p);
                                StringBuffer deseBuffer = new StringBuffer();
                                int st = 0;
                                for (Map.Entry<String, List<String>> entry : v.entrySet()) {
                                    if (st != 0) {
                                        deseBuffer.append(",");
                                    }
                                    deseBuffer.append(entry.getKey()).append("[");
                                    List<String> valList = entry.getValue();
                                    for (int j = 0; j < valList.size(); j++) {
                                        if (j != 0) {
                                            deseBuffer.append(",");
                                        }
                                        deseBuffer.append(valList.get(j));
                                    }
                                    deseBuffer.append("]");
                                }
                                singleDetail.put("deseName", deseBuffer.toString());
                                detailRet.add(singleDetail);
                            });
                            ret.put(currName, detailRet);
                        }
                    } else {
                        if ("1".equals(isCode)) {
                            String codeType = k.get(2);
                            String newVal = featureCodeService.acquireValueByCodeAndTypeId(codeType, String.valueOf(val));
                            ret.put(currName, newVal);
                        } else {
                            ret.put(currName, val);
                        }
                    }
                }
            }
        }
        return ret;
    }

    private <T> List<T> subListChildren(int page, int pageSize, List<T> allCacheList) {
        List<T> subChildrenList = Lists.newArrayList();
        if (allCacheList != null && allCacheList.size() > 0) {
            int totalPageCount = allCacheList.size() % pageSize > 0 ? (allCacheList.size() / pageSize) + 1 : allCacheList.size() / pageSize;
            //取出前端需要的值
            if (page == totalPageCount) {
                subChildrenList = allCacheList.subList(pageSize * (page - 1), allCacheList.size());
            } else {
                subChildrenList = allCacheList.subList(pageSize * (page - 1), pageSize * page);
            }
        }
        return subChildrenList;
    }

/**
 *  探针设备
 */

    /**============================用户行为日志==========================**/

    /**
     * 获取所有的用户行为日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getUserBehaviorLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的用户行为日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getUserBehaviorLog(Data dataMap) throws Exception {
        return null;
    }


    /**============================特权操作日志==========================**/

    /**
     * 获取所有的特权操作日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getPrivilegedOperationLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的特权操作日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getPrivilegedOperationLog(Data dataMap) throws Exception {
        return null;
    }


    /**============================查询工具日志==========================**/

    /**
     * 获取所有的查询工具日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getQueryToolLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的查询工具日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getQueryToolLog(Data dataMap) throws Exception {
        return null;
    }

    /*****************
     ****脱敏设备日志**
     *****************/

    /**============================数据库静态脱敏日志==========================**/


    /**
     * 获取所有的数据库静态脱敏日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getDataBaseStaticDesensitizationLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的数据库静态脱敏日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getDataBaseStaticDesensitizationLog(Data dataMap) throws Exception {
        return null;
    }
    /**==========================数据库动态脱敏日志============================**/

    /**
     * 获取所有的数据库动态脱敏日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getDataBaseDynamicDesensitizationLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的数据库动态脱敏日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getDataBaseDynamicDesensitizationLog(Data dataMap) throws Exception {
        return null;
    }
    /**======================文件静态脱敏日志=========================**/

    /**
     * 获取所有的文件静态脱敏日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getFileStaticDesensitizationLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的文件静态脱敏日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getFileStaticDesensitizationLog(Data dataMap) throws Exception {
        return null;
    }
    /**==========================文件动态脱敏日志===================**/

    /**
     * 获取所有的文件动态脱敏日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getFileDynamicDesensitizationLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的文件动态脱敏日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getFileDynamicDesensitizationLog(Data dataMap) throws Exception {
        return null;
    }


    /*****************
     ***运维设备日志***
     *************/


    /**============================运维操作日志==========================**/

    /**
     * 获取所有的运维操作日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getMaintenanceOperationLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的运维操作日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getMaintenanceOperationLog(Data dataMap) throws Exception {
        return null;
    }

    /*************
     ***系统日志***
     *************/


    /**============================监管平台日志==========================**/


    /**
     * 获取所有的监管平台日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getRegulatoryPlatformLogByPage(Data dataMap) throws Exception {
        return null;
    }


    /**
     * 获取所有的监管平台日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getRegulatoryPlatformLog(Data dataMap) throws Exception {
        return null;
    }
    /**============================探针设备日志==========================**/

    /**
     * 获取所有的探针设备日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getProbeEquipmentLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的探针设备日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getProbeEquipmentLog(Data dataMap) throws Exception {
        return null;
    }
    /**============================脱敏设备日志==========================**/

    /**
     * 获取所有的脱敏设备日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getDesensitizationEquipmentLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的脱敏设备日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getDesensitizationEquipmentLog(Data dataMap) throws Exception {
        return null;
    }
    /**============================运维设备日志==========================**/

    /**
     * 获取所有的运维设备日志 分页
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public Page getMaintenanceEquipmentLogByPage(Data dataMap) throws Exception {
        return null;
    }

    /**
     * 获取所有的运维设备日志
     *
     * @param dataMap
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, String>> getMaintenanceEquipmentLog(Data dataMap) throws Exception {
        return null;
    }
}
